﻿using FlexFlow.Base.Interface;
using FlexFlow.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FlexFlow.Base.Application
{
    /// <summary>
    /// 表示抽象的应用程序，该类中包含了一系列应用程序的基础封装，工具操作
    /// </summary>
    public abstract class AbstractApplication : IWorkFlowApplication
    {
        /// <summary>
        /// 表示活动表单数据槽
        /// </summary>
        public static DataSlot ActivityFormDataSlot = new DataSlot()
        {
            Identity = new Guid("AF05A608-8178-4A03-9EDE-ED16A49F17A7"),
            Name = "活动表单",
            Type = "Form",
            Required = true
        };
        /// <summary>
        /// 创建一个新的工作流任务工厂
        /// </summary>
        /// <returns></returns>
        protected WorkFlowTaskFactory CreateTaskFactory()
        {
            return new WorkFlowTaskFactory(this);
        }
        /// <summary>
        /// 检查用户进入数据
        /// </summary>
        /// <param name="context">
        /// 上下文
        /// </param>
        /// <param name="result">
        /// 操作结果
        /// </param>
        /// <returns>
        /// 一个值，表示是否应该中断应用程序的执行
        /// </returns>
        private bool CheckEnterData(ApplicationInvokeContext context, ApplicationExecuteResult result)
        {
            return true;
        }
        /// <summary>
        /// 预处理执行过程
        /// </summary>
        /// <param name="context">
        /// 执行上下文
        /// </param>
        /// <param name="result">
        /// 执行结果
        /// </param>
        /// <returns>
        /// 一个值，表示是否中断应用程序的执行
        /// </returns>
        private bool PreProcessorExecute(ApplicationInvokeContext context, ApplicationExecuteResult result)
        {
            return true;
        }
        /// <summary>
        /// 表示工作流任务的处理函数
        /// </summary>
        public delegate void TaskSubmitProcessAction(ApplicationInvokeContext context, WorkFlowTask task, TaskSubmitResult result);
        /// <summary>
        /// 执行应用程序
        /// </summary>
        /// <param name="context">
        /// 应用程序执行上下文
        /// </param>
        /// <returns>
        /// 应用程序执行结果
        /// </returns>
        public ApplicationExecuteResult Execute(ApplicationInvokeContext context)
        {
            //构建执行结果
            ApplicationExecuteResult result = new ApplicationExecuteResult();
            //执行
            OnExecute(context, result);
            //返回结果
            return result;
        }
        /// <summary>
        /// 处理任务提交
        /// </summary>
        /// <param name="context">
        /// 应用程序执行上下文
        /// </param>
        /// <param name="task">
        /// 提交的任务
        /// </param>
        /// <returns>
        /// 任务提交结果
        /// </returns>
        public TaskSubmitResult ProcessTaskSubmit(ApplicationInvokeContext context, WorkFlowTask task)
        {
            //构建执行结果
            TaskSubmitResult result = new TaskSubmitResult();
            //如果指定了处理函数
            if (task.SendData.ContainsKey("TaskSubmitProcessor"))
            {
                //构建处理函数
                TaskSubmitProcessAction act = Delegate.CreateDelegate(typeof(TaskSubmitProcessAction), this, (string)task.SendData["TaskSubmitProcessor"]) as TaskSubmitProcessAction;
                if (act != null)
                {
                    //调用处理函数
                    act(context, task, result);
                    //返回结果
                    return result;
                }
                else
                {
                    throw new InvalidOperationException("任务指定的处理函数不存在！");
                }
            }

            //执行
            OnTaskSubmit(context, task, result);
            //返回结果
            return result;
        }
        /// <summary>
        /// 执行应用程序
        /// </summary>
        /// <param name="context">
        /// 应用程序执行上下文
        /// </param>
        /// <param name="result">
        /// 应用程序执行结果
        /// </param>
        public abstract void OnExecute(ApplicationInvokeContext context, ApplicationExecuteResult result);
        /// <summary>
        /// 处理任务提交
        /// </summary>
        /// <param name="context">
        /// 应用程序执行上下文
        /// </param>
        /// <param name="task">
        /// 提交的任务
        /// </param>
        /// <param name="result">
        /// 任务提交结果
        /// </param>
        public abstract void OnTaskSubmit(ApplicationInvokeContext context, WorkFlowTask task, TaskSubmitResult result);
        /// <summary>
        /// 取得活动的数据插槽列表
        /// </summary>
        /// <param name="options">
        /// 活动定义的选项键值对，其中键位设定名，值为设定值的json串
        /// </param>
        /// <returns>
        /// 活动的数据插槽列表
        /// </returns>
        public virtual DataSlot[] GetDataSlots(KeyValuePair<string, string>[] options)
        {
            Dictionary<string, string> dic = options.ToDictionary(m => m.Key, m => m.Value);
            //表单模板
            if (dic.ContainsKey("formTemplate"))
            {
                ActivityFormDataSlot.Tag = dic["formTemplate"];
                return new DataSlot[] { ActivityFormDataSlot };
            }
            return new DataSlot[] { };
        }
        /// <summary>
        /// 获取条件满足的路径集合
        /// </summary>
        /// <param name="paths">
        /// 路径集合
        /// </param>
        /// <param name="condition">
        /// 条件
        /// </param>
        /// <param name="val">
        /// 条件的值
        /// </param>
        /// <returns>
        /// 条件满足的路径集合
        /// </returns>
        protected FlowPath[] GetConditionMatchedPath(FlowPath[] paths,string condition, string val)
        {
            return paths.Where(m => m.FlowCondition == condition.Trim() + ":" + val.Trim()).ToArray();
        }
    }
}
